package org.sky.service.serviceImpl;

import org.sky.base.DaoSupport;
import org.sky.model.Meal;
import org.sky.service.MealService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-10-23
 * Time: 下午1:51
 * To change this template use File | Settings | File Templates.
 */

@Service
@Transactional
public class MealServiceImpl extends DaoSupport<Meal> implements MealService {

    /**
     * 查找房间价格
     *
     * @param roomStyle 房间类型
     * @param mealName  套餐名字
     * @return
     */
    public double findPrice(String roomStyle, String mealName) {
        Query query = em.createQuery("from Meal m where m.mealName = ?1 ");
        query.setParameter(1, mealName);
        Meal ru = (Meal) query.getResultList().get(0);

        if ("情侣房".equals(roomStyle)) {
            return ru.getCoupleRoomPrice();
        } else if ("标准房".equals(roomStyle)) {
            return ru.getStandardRoomPrice();
        } else if ("中房".equals(roomStyle)) {
            return ru.getMiddleRoomPrice();
        } else if ("大房".equals(roomStyle)) {
            return ru.getBigRoomPrice();
        } else if ("VIP房".equals(roomStyle)) {
            return ru.getVipRoomPrice();
        } else return 0.0d;
    }

    /**
     * 修改房价
     *
     * @param roomStyle 房间类型
     * @param mealName  套餐名字
     * @param price     价格
     * @return
     */
    public boolean setPrice(String roomStyle, String mealName, double price) {
        Query query = em.createQuery("from Meal m where m.mealName = ?1 ");
        query.setParameter(1, mealName);
        Meal ru = (Meal) query.getResultList().get(0);

        if ("情侣房".equals(roomStyle)) {
            ru.setCoupleRoomPrice(price);
        } else if ("标准房".equals(roomStyle)) {
            ru.setStandardRoomPrice(price);
        } else if ("中房".equals(roomStyle)) {
            ru.setMiddleRoomPrice(price);
        } else if ("大房".equals(roomStyle)) {
            ru.setBigRoomPrice(price);
        } else if ("VIP房".equals(roomStyle)) {
            ru.setVipRoomPrice(price);
        } else return false;

        try {
            update(ru);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
